//
//  XMMineHeaderView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/10.
//

#import "XMMineHeaderView.h"
#import "XMPropertyInfoView.h"

@interface XMMineHeaderView ()<XMPropertyInfoViewDelegate>

@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *userIdLabel;  //用户ID
@property (nonatomic, strong) UIButton *QRCodeBtn;  //二维码
@property (nonatomic, strong) UIButton *authenBtn;  //认证
@property (nonatomic, strong) UILabel *briefIntroucLabel;  //简介

@property (nonatomic, strong) UILabel *praiseNumLabel; //获赞
@property (nonatomic, strong) UILabel *followEachOtherNumLabel; //互关
@property (nonatomic, strong) UILabel *attentionNumLabel;  //关注
@property (nonatomic, strong) UILabel *fansNumLabel;  //粉丝

@end

@implementation XMMineHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, KScaleWidth(612.0))];
    [titleView setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0x7A6A3F).CGColor, (__bridge id)kColorHEX(0x817A66).CGColor] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1)];
    [self addSubview:titleView];
    self.clipsToBounds = YES;
    
    [self addSubview:self.avatarImgView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.authenBtn];
    [self addSubview:self.userIdLabel];
    [self addSubview:self.briefIntroucLabel];
    [self addSubview:self.QRCodeBtn];
    _QRCodeBtn.x = _userIdLabel.maxX + KScaleWidth(6.0);
    
    NSArray *titleArr = @[@"获赞", @"互关", @"关注", @"粉丝"];
    for (int i = 0; i < 4; i ++) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((KScaleWidth(24.0) + KScaleWidth(28.0))*i, _briefIntroucLabel.maxY + KScaleWidth(28.0), KScaleWidth(24.0) + KScaleWidth(28.0), KScaleWidth(20.0)) font:KFontSize(14.0) textColor:kColorHEXA(0xffffff, 0.84) textAlignment:NSTextAlignmentCenter];
        titleLabel.text = titleArr[i];
        [self addSubview:titleLabel];
    }
    [self addSubview:self.praiseNumLabel];
    [self addSubview:self.followEachOtherNumLabel];
    [self addSubview:self.attentionNumLabel];
    [self addSubview:self.fansNumLabel];
    
    UIButton *editHomePageBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(15.0) frame:CGRectMake(self.width - KScaleWidth(16.0) - KScaleWidth(86.0), _briefIntroucLabel.maxY + KScaleWidth(10.0), KScaleWidth(86.0), KScaleWidth(32.0)) title:@"编辑主页" titleColor:kColorHEX(0xffffff)];
    [editHomePageBtn setRadius:editHomePageBtn.height/2 borderColor:kColorHEX(0xffffff) borderWidth:1];
    editHomePageBtn.backgroundColor = kColorHEXA(0xffffff, 0.13);
    [editHomePageBtn addTarget:self action:@selector(homePageEditAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:editHomePageBtn];
    
    XMPropertyInfoView *propertyInfoView = [[XMPropertyInfoView alloc] initWithFrame:CGRectMake(0, editHomePageBtn.maxY + KScaleWidth(20.0), self.width, KScaleWidth(262.0))];
    propertyInfoView.delegate = self;
    [self addSubview:propertyInfoView];
}

- (void)resetInfoWithUserInfoModel:(XMUserInfoModel *)userInfoModel {
    if (!userInfoModel) {
        _QRCodeBtn.hidden = YES;
        _authenBtn.hidden = YES;
        return;
    }
    _QRCodeBtn.hidden = NO;
    _authenBtn.hidden = NO;
    XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:userInfoModel.avatar]];
    _nameLabel.text = userModel.nickName;
    _userIdLabel.text = [NSString stringWithFormat:@"ID: %@", userInfoModel.idNo];
    CGFloat userIdWidth = [_userIdLabel.text getWidthWithFontSize:_userIdLabel.font];
    _QRCodeBtn.x = _userIdLabel.x + userIdWidth + KScaleWidth(10.0);
    
    CGFloat width = [userInfoModel.nickname getWidthWithFontSize:KBoldFontSize(18.0)];
    _authenBtn.x = _nameLabel.x + width + KScaleWidth(6.0);
    _authenBtn.selected = [userInfoModel.certifyStatus isEqualToString:@"1"];//YES;//userInfoModel.certifyStatus;
    
    _praiseNumLabel.text = userInfoModel.likeCount;
    _followEachOtherNumLabel.text = userInfoModel.huguanCount;
    _attentionNumLabel.text = userInfoModel.attentionCount;
    _fansNumLabel.text = userInfoModel.fensi;
    _briefIntroucLabel.text = userInfoModel.briefly;
}

- (void)homePageEditAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(homePageEditAction)]) {
        [self.delegate homePageEditAction];
    }
}

#pragma mark - BtnAction
//去认证
- (void)authenAction:(UIButton *)button {
    if (!button.selected) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(authenAction)]) {
            [self.delegate authenAction];
        }
    }
}

//查看二维码
- (void)qrcodeCheckAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(qrcodeCheckAction)]) {
        [self.delegate qrcodeCheckAction];
    }
}

#pragma mark - Delegate
//查看明细
- (void)checkDetail {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkEarnDetail)]) {
        [self.delegate checkEarnDetail];
    }
}

//提现
- (void)putForward {
    if (self.delegate && [self.delegate respondsToSelector:@selector(putForwardMoney)]) {
        [self.delegate putForwardMoney];
    }
}

//查看订单列表
- (void)checkOrderListWithOrderType:(NSInteger)orderType {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkMyOrderListWithOrderType:)]) {
        [self.delegate checkMyOrderListWithOrderType:orderType];
    }
}

#pragma mark - Init
- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(14.0), KScaleWidth(103.0), KScaleWidth(78.0), KScaleWidth(78.0))];
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
        _avatarImgView.image = ImageNamed(@"mine_avatar_icon");
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(107.0), KScaleWidth(112.0), self.width - KScaleWidth(107.0), KScaleWidth(35.0)) font:KBoldFontSize(18.0) textColor:kColorHEX(0xffffff)];
    }
    return _nameLabel;
}

- (UIButton *)authenBtn {
    if (!_authenBtn) {
        _authenBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(self.nameLabel.maxX, _nameLabel.y, KScaleWidth(64.0), _nameLabel.height) image:@"mine_authentication_icon" selectImg:@"mine_authentication_icon_select"];
        [_authenBtn addTarget:self action:@selector(authenAction:) forControlEvents:UIControlEventTouchUpInside];
//    initWithFrame:CGRectMake(self.nameLabel.maxX + KScaleWidth(6.0), _nameLabel.y, KScaleWidth(52.0), _nameLabel.height)];
    }
    return _authenBtn;
}

- (UILabel *)userIdLabel {
    if (!_userIdLabel) {
        _userIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.x, _nameLabel.maxY, _nameLabel.width, KScaleWidth(17.0)) font:KFontSize(12.0) textColor:kColorHEXA(0xffffff, 0.6)];
    }
    return _userIdLabel;
}

//二维码
- (UIButton *)QRCodeBtn {
    if (!_QRCodeBtn) {
        _QRCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(0, self.userIdLabel.y, KScaleWidth(24.0), _userIdLabel.height) image:@"mine_qrcode_icon" selectImg:@"mine_qrcode_icon"];
        [_QRCodeBtn addTarget:self action:@selector(qrcodeCheckAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _QRCodeBtn;
}

//简介
- (UILabel *)briefIntroucLabel {
    if (!_briefIntroucLabel) {
        _briefIntroucLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(13.0), self.avatarImgView.maxY + KScaleWidth(15.0), self.width - KScaleWidth(13.0), KScaleWidth(30.0)) font:KFontSize(14.0) textColor:kColorHEXA(0xffffff, 0.84)];
        _briefIntroucLabel.text = @"点击这里填写简介";
    }
    return _briefIntroucLabel;
}

- (UILabel *)praiseNumLabel {
    if (!_praiseNumLabel) {
        _praiseNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.briefIntroucLabel.maxY, KScaleWidth(24.0) + KScaleWidth(28.0), KScaleWidth(25.0)) font:KBoldFontSize(18.0) textColor:kColorHEX(0xffffff) textAlignment:NSTextAlignmentCenter];
        _praiseNumLabel.text = @"--";
    }
    return _praiseNumLabel;
}

- (UILabel *)followEachOtherNumLabel {
    if (!_followEachOtherNumLabel) {
        _followEachOtherNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.praiseNumLabel.maxX, _praiseNumLabel.y, _praiseNumLabel.width, _praiseNumLabel.height) font:KBoldFontSize(18.0) textColor:kColorHEX(0xffffff) textAlignment:NSTextAlignmentCenter];
        _followEachOtherNumLabel.text = @"--";
    }
    return _followEachOtherNumLabel;
}

- (UILabel *)attentionNumLabel {
    if (!_attentionNumLabel) {
        _attentionNumLabel = [[UILabel alloc] initWithFrame:self.followEachOtherNumLabel.frame font:KBoldFontSize(18.0) textColor:kColorHEX(0xffffff) textAlignment:NSTextAlignmentCenter];
        _attentionNumLabel.x = _followEachOtherNumLabel.maxX;
        _attentionNumLabel.text = @"--";
    }
    return _attentionNumLabel;
}

- (UILabel *)fansNumLabel {
    if (!_fansNumLabel) {
        _fansNumLabel = [[UILabel alloc] initWithFrame:self.attentionNumLabel.frame font:KBoldFontSize(18.0) textColor:kColorHEX(0xffffff) textAlignment:NSTextAlignmentCenter];
        _fansNumLabel.x = _attentionNumLabel.maxX;
        _fansNumLabel.text = @"--";
    }
    return _fansNumLabel;
}

@end
